<?php

// +----------------------------------------------------------------------
// |新闻控制器
// +----------------------------------------------------------------------
// | 2018-1-4 @wen
// +----------------------------------------------------------------------

namespace Admin\Controller;

use \lyf\Tree;

/**
 * 控制器
 * @author jry <598821125@qq.com>
 */
class NewsController extends AdminController {

    /**
     * 列表
     * @author
     */
    public function index($group = '1') {

        // 获取所有导航
        $map['status'] = array('egt', '0');
        $map['type_id']  = $group;
        $data_list     = D('AdminNews')
                    ->where($map)
                    ->order('sort asc, id asc')
                    ->select();


        // 使用Builder快速建立列表页面
        $builder = new \lyf\builder\ListBuilder();
        $builder->setMetaTitle('新闻列表') // 设置页面标题
                ->addTopButton('addnew', array('href' => U('add', array('group' => $group)))) // 添加新增按钮
                ->addTopButton('delete') // 添加删除按钮
                ->addTableColumn('id', 'ID')
                ->addTableColumn('title', '标题')
                ->addTableColumn('cover', '封面', 'picture')
                ->addTableColumn('sort', '排序')
                ->addTableColumn('status', '状态', 'status')
                ->addTableColumn('right_button', '操作', 'btn')
                ->setTableDataList($data_list) // 数据列表
                ->addRightButton('edit', array('href' => U('edit', array('group' => $group, 'id' => '__data_id__')))) // 添加编辑按钮
                ->addRightButton('delete') // 添加删除按钮
                ->display();
    }


    /**
     * 新增
     * @author
     */
    public function add($group) {

        if (request()->isPost()) {
            $nav_object = M('AdminNews');
            $data = $nav_object->create();
            if ($data) {
                $id = $nav_object->add($data);
                if ($id) {
                    $this->success('新增成功', U('index', array('group' => $group)));
                } else {
                    $this->error('新增失败：' . $nav_object->getError());
                }
            } else {
                $this->error($nav_object->getError());
            }
        } else {
            // 获取前台模版供选择
            if (C('CURRENT_THEME')) {
                $template_list = \lyf\File::get_dirs(getcwd() . '/Theme/' . C('CURRENT_THEME') . '/Home//Nav');
            } else {
                $template_list = \lyf\File::get_dirs(getcwd() . '/Application/Home/View/Nav');
            }
            foreach ($template_list['file'] as $val) {
                $val = substr($val, 0, -5);
                if (strstr($val, 'lists')) {
                    $template_list_index[$val] = $val;
                } elseif (strstr($val, 'detail')) {
                    $template_list_detail[$val] = $val;
                }
            }


            // 使用FormBuilder快速建立表单页面
            $builder = new \lyf\builder\FormBuilder();
            $builder->setMetaTitle('新增导航') // 设置页面标题
                    ->setPostUrl(U('', array('group' => $group))) // 设置表单提交地址
                    ->addFormItem('title', 'num', '新闻标题', '新闻标题')
                    ->addFormItem('cover', 'picture', '新闻封页', '新闻封页')
                    ->addFormItem('the_profile', 'num', '新闻概况', '新闻概况')
                    ->addFormItem('value', 'summernote', '新闻内容', '新闻内容')
                    ->addFormItem('type_id', 'select', '新闻类型', '新闻类型', array('1' => '公司新闻', '2' => '行内新闻', '3' => '公司公告'))
                    ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
                    ->setFormData(array('type' => 'link', 'group' => $group))
                    ->setExtraHtml($this->extra_html)
                    ->display();
        }
    }

    /**
     * 编辑
     * @author
     */
    public function edit($id, $group)
    {
        if (request()->isPost()) {
            $nav_object = M('AdminNews');
            $data = $nav_object->create();

            if ($data) {
                if ($nav_object->save($data)) {
                    $this->success('更新成功', U('index', array('group' => $group)));
                } else {
                    $this->error('更新失败：' . $nav_object->getError());
                }
            } else {
                $this->error($nav_object->getError());
            }
        } else {
            $info = D('AdminNews')->find($id);
            // 获取前台模版供选择
            if (C('CURRENT_THEME')) {
                $template_list = \lyf\File::get_dirs(getcwd() . '/Theme/' . C('CURRENT_THEME') . '/Home//Nav');
            } else {
                $template_list = \lyf\File::get_dirs(getcwd() . '/Application/Home/View/Nav');
            }
            foreach ($template_list['file'] as $val) {
                $val = substr($val, 0, -5);
                if (strstr($val, 'lists')) {
                    $template_list_index[$val] = $val;
                } elseif (strstr($val, 'detail')) {
                    $template_list_detail[$val] = $val;
                }
            }

            // 使用FormBuilder快速建立表单页面
            $builder = new \lyf\builder\FormBuilder();
            $builder->setMetaTitle('编辑新闻') // 设置页面标题
                    ->setPostUrl(U('', array('group' => $group))) // 设置表单提交地址
                    ->addFormItem('id', 'hidden', 'ID', 'ID')
                    ->addFormItem('title', 'num', '新闻标题', '新闻标题','',array('must' => 1))
                    ->addFormItem('cover', 'picture', '新闻封页', '新闻封页')
                    ->addFormItem('the_profile', 'num', '新闻概况', '新闻概况')
                    ->addFormItem('value', 'summernote', '新闻内容', '新闻内容')
                    ->addFormItem('type_id', 'select', '新闻类型', '新闻类型', array('1' => '公司新闻', '2' => '行内新闻', '3' => '公司公告'))
                    ->addFormItem('sort', 'num', '排序', '用于显示的顺序')
                    ->setFormData($info)
                    ->display();
        }
    }
}
